package com.sfx.算法专题.滑动窗口.变化窗口;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-08-05
 * Time: 15:04
 */
public class Code_2_lengthOfLongestSubstring {

    /**
     *
     * 基本方法 : 暴力枚举 + 哈希表
     *          暴力枚举用于枚举所有子串,哈希表用于判断两指针区间内有没有重复字符
     * 暴力枚举优化点 :
     */
    static class Solution {
        public int lengthOfLongestSubstring(String s) {
            int left = 0;
            int right = 0;
            int res = 0;
            int[] hash = new int[128];
            while(right < s.length()) {
                //进窗口
                hash[s.charAt(right)] ++;
                //出窗口
                while(hash[s.charAt(right)] > 1) {
                    hash[s.charAt(left++)] --;
                }
                // right不能在这里,如果right++,又出现重复字符了呢
                res = Math.max(res,right - left+1);
                right ++;
            }
            return res;
        }
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.lengthOfLongestSubstring("abcabcbb");
    }
}